package PTA.练习0503;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-11-15
 * Time: 9:05
 */
public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        // 这里半径的类型是int可以吗？
        int r1, r2, r3, r4, r5;
        r1 = input.nextInt();
        Circle c1 = new Circle(r1);
        r2 = input.nextInt();
        Circle c2 = new Circle(r2);
        r3 = input.nextInt();
        Circle c3 = new Circle(r3);
        r4 = input.nextInt();
        Circle c4 = new Circle(r4);
        r5 = input.nextInt();
        Circle c5 = new Circle(r5);
        input.close();

        // 为什么这里是2，后面却插入了5次？
        CircleList circleList = new CircleList(2);
        circleList.addAt(0, c1);
        System.out.println(circleList);
        circleList.addAt(1, c2);
        System.out.println(circleList);
        circleList.addAt(1, c3);
        System.out.println(circleList);
        circleList.addAt(2, c4);
        System.out.println(circleList);
        circleList.addAt(0, c5);
        System.out.println(circleList);
        circleList.removeAt(0);
        System.out.println(circleList);
        circleList.removeAt(2);
        System.out.println(circleList);
        if(circleList instanceof Sortable) {
            // 为什么这里可以直接println一个字符串+对象？
            System.out.println("The maximum circle is: " + circleList.max());
            System.out.println("The minimum circle is: " + circleList.min());
        }
    }
}
class Circle implements Comparable<Circle>{
    double radius;
    Circle(double radius){
        this.radius=radius;
    }
    //用于返回圆的面积
    public double getArea(){
        return Math.PI*radius*radius;
    }
    //用于返回圆的半径
    public double getRadius() {
        return radius;
    }

    @Override
    //以面积的大小作为比较的依据
    public int compareTo(Circle o) {
        return (int) (radius-o.radius);
    }

    @Override
    public String toString() {
        return "Circle(r="+radius+")";
    }
}

class CircleList implements Sortable<Circle>{
    int size=0;
    int capacity;
    Circle[] array;
    //预留一个大小为capacity的存储空间
    CircleList(int capacity){
        this.capacity=capacity;
        array= new Circle[this.capacity];
    }
    //在指定的index处插入对象c
    void addAt(int index, Circle c){
        dilatation();
        for (int i = size; i >index; i--) {
            array[i]=array[i-1];
        }
        array[index]=c;
        size++;
    }
    //移除指定index处的对象
    void removeAt(int index){
        for (int i = index; i < size-1; i++) {
            array[i]=array[i+1];
        }
        size--;
    }
    //用于获取当前容器中存放的对象的数量
    int getSize(){
        return this.size;
    }
    //扩容
    void dilatation(){
        if (size == capacity) {
            Circle[] newArray = new Circle[capacity * 2];
            System.arraycopy(array, 0, newArray, 0, size);
            array = newArray;
            capacity *= 2;
        }
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder= new StringBuilder("[");
        stringBuilder.append(array[0]);
        for (int i = 1; i < size; i++) {
            stringBuilder.append(", "+array[i]);
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    @Override
    public Circle max() {
        Circle max=array[0];
        for (int i = 1; i < size; i++) {
            if(array[i].getArea()>max.getArea()){
                max=array[i];
            }
        }
        return max;
    }

    @Override
    public Circle min() {
        Circle min=array[0];
        for (int i = 1; i < size; i++) {
            if(array[i].getArea()<min.getArea()){
                min=array[i];
            }
        }
        return min;
    }
}
interface Sortable<T> {
    public T max();
    public T min();
}
